﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Web.Security;
using BLL;

public partial class SubjectLogs : System.Web.UI.Page
{
    SecurityEvaluateDataContext db = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        db = new SecurityEvaluateDataContext();
        if (!IsPostBack)
        {
            ListPageContral1.SetFirstPage();
            BindProblem(0);
        }

    }

    void BindProblem(int PageIndex)
    {
        List<DAL.SecuritySubject> logs = BLL.Security.getsubjectLogsByUserID(User.Identity.Name);
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        var result2 = Security2.getsubjectLogsByUserID(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);
        // var result = (from obj in db.Examination where obj.DepartmentID == DepartmentID select obj).ToList();

        foreach (DAL2.SecuritySubject ex in result2)
        {
            logs.Add(new DAL.SecuritySubject() { Caption=ex.Caption, CreateDepartment=ex.CreateDepartment, CreateTime=ex.CreateTime, CreateUser=ex.CreateUser, Flag=ex.Flag, GuardCount=ex.GuardCount, HasChild=ex.HasChild, Id=ex.Id, OverDate=ex.OverDate, ParentID=ex.ParentID, Remak=ex.Remak });
        }

        ListPageContral1.ItemCount = logs.Count();
        GV.DataSource = logs.Skip(PageIndex * ListPageContral1.PageSize).Take(ListPageContral1.PageSize);
        GV.DataBind();
    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        BindProblem(PageIndex);
    }

    public string GetPageUrl(object obj)
    {
        if (obj == null)
            return "";
        Question2DataContext db1 = new Question2DataContext();
        var result = (from o in db1.Examination2 where o.SID == new Guid(obj.ToString()) select o).FirstOrDefault();
        if (result == null)
            return "problemsResult2.aspx";
        else
            return "problemsResult.aspx";


    }

    protected void gvChemical_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            SecurityEvaluateDataContext db = new SecurityEvaluateDataContext();
            Label lblID = e.Row.FindControl("lblID") as Label;

            lblID.Text = Convert.ToString(GV.PageSize * ListPageContral1.CurrentPageIndex + (e.Row.RowIndex + 1));
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            //DAL.SecuritySubject subject = e.Row.DataItem as DAL.SecuritySubject;

            //DAL.Examination exam = BLL.Security.getExamationBySubject(subject.Id, User.Identity.Name);

            //if (exam != null)
            //{

            //    Label lblCaption = e.Row.FindControl("lblCaption") as Label;
            //    lblCaption.Text = subject.Caption;


            //    Label lblDepartment = e.Row.FindControl("lblDepartment") as Label;

            //    string departmentid =Yipusoft.Account.BLL.User.GetUser(exam.SetUserID).DepartmentID;
            //    lblDepartment.Text = BLL.Department.GetDepartmentByID(departmentid).ShortName;

            //    DateTime? lastTime = Security.getLastResultDate(User.Identity.Name, subject.Id);
            //    Label lblDate = e.Row.FindControl("lblDate") as Label;
            //    if (lastTime.HasValue)
            //    {
            //        lblDate.Text = lastTime.Value.ToString("yyyy-MM-dd HH:mm");
            //    }
            //}
            Label lblDepartment = e.Row.FindControl("lblDepartment") as Label;
            DAL.SecuritySubject subject = e.Row.DataItem as DAL.SecuritySubject;
            DAL.Examination exam = BLL.Security.getExamationBySubject(new Guid(GV.DataKeys[e.Row.RowIndex].Value.ToString()), User.Identity.Name);
            Label lblCaption = e.Row.FindControl("lblCaption") as Label;
            if (exam != null)
            {
                lblCaption.Text = subject.Caption;
                DAL.FireMisDataContext fdb = new FireMisDataContext();
                string departmentid = fdb.Account_Users.Where(o => o.UserID == exam.SetUserID).FirstOrDefault().DepartmentID;
                lblDepartment.Text = BLL.Department.GetDepartmentByID(departmentid).ShortName;
            }
            else
            {
                QuestionDataContext db1 = new QuestionDataContext();


                DAL.SecuritySubject subject2 = e.Row.DataItem as DAL.SecuritySubject;
                DAL2.Examination exam2 = BLL.Security2.getExamationBySubject(new Guid(GV.DataKeys[e.Row.RowIndex].Value.ToString()), Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);
                DAL.FireMisDataContext fdb = new FireMisDataContext();
                string departmentid = fdb.Account_Users.Where(o => o.UserID == exam2.SetUserID).FirstOrDefault().DepartmentID;
                lblDepartment.Text = BLL.Department.GetDepartmentByID(departmentid).ShortName;
                if (exam2 != null)
                {
                    lblCaption.Text = subject2.Caption;
                }
            }
        }
    }
}
